package com.platform.service.impl;

import com.platform.dao.UserAssetDao;
import com.platform.entity.*;
import com.platform.entity.excel.UserAssetExcel;
import com.platform.service.*;
import com.platform.util.CoinTypeEnum;
import com.platform.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会员管理:用户资产Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2019-02-01 17:31:55
 */
@Service("userAssetService")
public class UserAssetServiceImpl implements UserAssetService {
    @Autowired
    private UserAssetDao userAssetDao;
    @Autowired
    private UserService userService;
    @Autowired
    private IncomeCoinTaskService incomeCoinTaskService;
    @Autowired
    private IncomeCoinService incomeCoinService;
    @Autowired
    private DynamicRuleService dynamicRuleService;

    @Override
    public UserAssetEntity queryObject(Integer id) {
        return userAssetDao.queryObject(id);
    }

    @Override
    public UserAssetEntity queryObjectForUid(long uid) {
        return userAssetDao.queryObjectForUid(uid);
    }

    @Override
    public boolean updateAsset(UserAssetEntity asset) {
        return userAssetDao.updateAsset(asset) > 0;
    }

    @Override
    public BigDecimal selectTotal() {
        return userAssetDao.selectTotal();
    }

    @Override
    public List<UserAssetExcel> queryExcelList(String name, String phone, Integer sort, Integer done, Integer index, Integer size) {

        Integer start = (index - 1) * size;
        return userAssetDao.queryExcelList(name, phone, sort, done, start, size);
    }


    // 天加精选奖励
    @Override
    @Transactional
    public boolean updateAsset(Long userid, Long newid) {

        UserEntity user = userService.queryObject(userid);
        DynamicRuleEntity conf = dynamicRuleService.selectForKey("choicenessContent");
        String date= DateUtils.format(new Date(),"yyyy-MM-dd");
        boolean result = incomeCoinTaskService.checkDynamic(userid,conf.getId(),date);
        if (result) {
            return true;
        }
        UserAssetEntity asset = userAssetDao.queryObjectForUid(userid);
        BigDecimal coin =  conf.getCreditcount().multiply(BigDecimal.valueOf(user.getLevel()));
        asset.setSpcCoin(asset.getSpcCoin().add(coin));
        int i = userAssetDao.updateAsset(asset);
        if (i <= 0) {
            return false;
        }
        IncomeCoinTaskEntity it = IncomeCoinTaskEntity.builder().name(conf.getName()).integral(coin).userid(userid).type(0).creditruleid(conf.getId()).build();
        incomeCoinTaskService.save(it);
        return true;
    }

    @Override
    public Integer updateUserAsset(Map<String, Object> param) {
        return userAssetDao.updateUserAsset(param);
    }

    @Override
    public Integer addCoin(Map<String, Object> param) {
        return userAssetDao.addCoin(param);
    }

    @Override
    public BigDecimal queryTotalEnergyTotal() {
        return userAssetDao.queryTotalEnergyTotal();
    }

    @Override
    public BigDecimal queryTotalSpcCoin() {
        return userAssetDao.queryTotalSpcCoin();
    }

    @Override
    public List<UserAssetEntity> queryList(Map<String, Object> map) {
//        String amount = String.valueOf(map.get("amount"));
//        String coin = String.valueOf(map.get("coin"));
//        String order = this.getOrderSql(amount, coin);
//        map.put("sort", order);
        return userAssetDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {

        String amount = String.valueOf(map.get("amount"));
        String coin = String.valueOf(map.get("coin"));

        return userAssetDao.queryTotal(map);
    }


    @Override
    public int save(UserAssetEntity tUserAsset) {
        return userAssetDao.save(tUserAsset);
    }

    @Override
    public int update(UserAssetEntity tUserAsset) {
        return userAssetDao.update(tUserAsset);
    }

    @Override
    public int delete(Integer id) {
        return userAssetDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return userAssetDao.deleteBatch(ids);
    }

}
